<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="C技术," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="编程的核心是数据结构，而不是算法 数据是易变的，逻辑是稳定的  函数表驱动法12345678910111213141516171819202122231 typedef struct&amp;#123; 2     DOUBLE  aRangeLimit; 3     CHAR    *pszGrade; 4 &amp;#125;T_GRADE_MAP; 5  6 T_GRADE_MAP gGradeMap[MA">
<meta name="keywords" content="C技术">
<meta property="og:type" content="article">
<meta property="og:title" content="编程的核心">
<meta property="og:url" content="http://yoursite.com/2017/08/19/编程的核心/index.html">
<meta property="og:site_name" content="linghaibin-Blog">
<meta property="og:description" content="编程的核心是数据结构，而不是算法 数据是易变的，逻辑是稳定的  函数表驱动法12345678910111213141516171819202122231 typedef struct&amp;#123; 2     DOUBLE  aRangeLimit; 3     CHAR    *pszGrade; 4 &amp;#125;T_GRADE_MAP; 5  6 T_GRADE_MAP gGradeMap[MA">
<meta property="og:updated_time" content="2017-08-20T08:57:53.671Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="编程的核心">
<meta name="twitter:description" content="编程的核心是数据结构，而不是算法 数据是易变的，逻辑是稳定的  函数表驱动法12345678910111213141516171819202122231 typedef struct&amp;#123; 2     DOUBLE  aRangeLimit; 3     CHAR    *pszGrade; 4 &amp;#125;T_GRADE_MAP; 5  6 T_GRADE_MAP gGradeMap[MA">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/08/19/编程的核心/"/>





  <title>编程的核心 | linghaibin-Blog</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">linghaibin-Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">坚持 坚持 坚持 ！！！</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-tags " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/08/19/编程的核心/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="linghaibin">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="linghaibin-Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">编程的核心</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-19T09:59:02-07:00">
                2017-08-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/c-jishu/" itemprop="url" rel="index">
                    <span itemprop="name">c jishu</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="编程的核心是数据结构，而不是算法"><a href="#编程的核心是数据结构，而不是算法" class="headerlink" title="编程的核心是数据结构，而不是算法"></a>编程的核心是数据结构，而不是算法</h1><blockquote>
<p>数据是易变的，逻辑是稳定的</p>
</blockquote>
<h1 id="函数表驱动法"><a href="#函数表驱动法" class="headerlink" title="函数表驱动法"></a>函数表驱动法</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="number">1</span> <span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span>&#123;</span></div><div class="line"> <span class="number">2</span>     DOUBLE  aRangeLimit;</div><div class="line"> <span class="number">3</span>     CHAR    *pszGrade;</div><div class="line"> <span class="number">4</span> &#125;T_GRADE_MAP;</div><div class="line"> <span class="number">5</span> </div><div class="line"> <span class="number">6</span> T_GRADE_MAP gGradeMap[MAX_GRADE_LEVEL] = &#123;</div><div class="line"> <span class="number">7</span>     &#123;<span class="number">50.0</span>,              <span class="string">"Fail"</span>&#125;,</div><div class="line"> <span class="number">8</span>     &#123;<span class="number">60.0</span>,              <span class="string">"Pass"</span>&#125;,</div><div class="line"> <span class="number">9</span>     &#123;<span class="number">70.0</span>,              <span class="string">"Credit"</span>&#125;,</div><div class="line"><span class="number">10</span>     &#123;<span class="number">80.0</span>,              <span class="string">"Distinction"</span>&#125;,</div><div class="line"><span class="number">11</span>     &#123;<span class="number">100.0</span>,             <span class="string">"High Distinction"</span>&#125;</div><div class="line"><span class="number">12</span> &#125;;</div><div class="line"><span class="number">13</span> </div><div class="line"><span class="number">14</span> <span class="function"><span class="keyword">static</span> CHAR* <span class="title">EvaluateGrade</span><span class="params">(DOUBLE dScore)</span></span></div><div class="line">15 &#123;</div><div class="line"><span class="number">16</span>     INT8U ucLevel = <span class="number">0</span>;</div><div class="line"><span class="number">17</span>     <span class="keyword">for</span>(; ucLevel &lt; MAX_GRADE_LEVEL; ucLevel++)</div><div class="line"><span class="number">18</span>     &#123;</div><div class="line"><span class="number">19</span>         <span class="keyword">if</span>(dScore &lt; gGradeMap[ucLevel].aRangeLimit)</div><div class="line"><span class="number">20</span>             <span class="keyword">return</span> gGradeMap[ucLevel].pszGrade;</div><div class="line"><span class="number">21</span>     &#125;</div><div class="line"><span class="number">22</span>     <span class="keyword">return</span> gGradeMap[<span class="number">0</span>].pszGrade;</div><div class="line"><span class="number">23</span> &#125;</div></pre></td></tr></table></figure>
<h3 id="字符统计"><a href="#字符统计" class="headerlink" title="字符统计"></a>字符统计</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div></pre></td><td class="code"><pre><div class="line"> <span class="number">1</span> <span class="comment">/* 日志控制类型定义 */</span></div><div class="line"> <span class="number">2</span> <span class="keyword">typedef</span> <span class="keyword">enum</span></div><div class="line"> <span class="number">3</span> &#123;</div><div class="line"> <span class="number">4</span>     LOG_NORM = <span class="number">0</span>,        <span class="comment">/* 未分类日志，可用于通用日志 */</span></div><div class="line"> <span class="number">5</span>     LOG_FRM,             <span class="comment">/* Frame，OMCI帧日志 */</span></div><div class="line"> <span class="number">6</span>     LOG_PON,             <span class="comment">/* Pon，光链路相关日志 */</span></div><div class="line"> <span class="number">7</span>     LOG_ETH,             <span class="comment">/* Ethernet，Layer2以太网日志 */</span></div><div class="line"> <span class="number">8</span>     LOG_NET,             <span class="comment">/* Internet，Layer3网络日志 */</span></div><div class="line"> <span class="number">9</span>     LOG_MULT,            <span class="comment">/* Multicast，组播日志 */</span></div><div class="line"><span class="number">10</span>     LOG_QOS,             <span class="comment">/* QOS，流量日志 */</span></div><div class="line"><span class="number">11</span>     LOG_CES,             <span class="comment">/* Ces，TDM电路仿真日志 */</span></div><div class="line"><span class="number">12</span>     LOG_VOIP,            <span class="comment">/* Voip，语音日志 */</span></div><div class="line"><span class="number">13</span>     LOG_ALM,             <span class="comment">/* Alarm，告警日志 */</span></div><div class="line"><span class="number">14</span>     LOG_PERF,            <span class="comment">/* Performance，性能统计日志 */</span></div><div class="line"><span class="number">15</span>     LOG_VER,             <span class="comment">/* Version，软件升级日志 */</span></div><div class="line"><span class="number">16</span>     LOG_XDSL,            <span class="comment">/* xDsl日志 */</span></div><div class="line"><span class="number">17</span>     LOG_DB,              <span class="comment">/* 数据库操作日志 */</span></div><div class="line"><span class="number">18</span>     <span class="comment">//新日志类型在此处扩展，共支持32种日志类型</span></div><div class="line"><span class="number">19</span>     LOG_ALL = UINT_MAX   <span class="comment">/* 所有日志类型 */</span></div><div class="line"><span class="number">20</span> &#125;E_LOG_TYPE;</div><div class="line"></div><div class="line"><span class="number">22</span> <span class="comment">/*****************************************************************************</span></div><div class="line">23  * 变量名称：gOmciLogCtrl</div><div class="line">24  * 作用描述：OMCI日志控制字，BitMap格式(比特编号从LSB至MSB依次为Bit0-&gt;BitN)。</div><div class="line">25  *           Bit0~N分别对应E_LOG_TYPE各枚举值(除LOG_ALL外)。</div><div class="line">26  *           BitX为0时关闭日志类型对应的日志功能，BitX为1时则予以打开。</div><div class="line">27  * 变量范围：该变量为四字节整型静态全局变量，即支持32种日志类型。</div><div class="line">28  * 访问说明：通过GetOmciLogCtrl/SetOmciLogCtrl/OmciLogCtrl函数访问/设置控制字。</div><div class="line">29  *****************************************************************************/</div><div class="line"><span class="number">30</span> <span class="keyword">static</span> INT32U gOmciLogCtrl = <span class="number">0</span>;</div><div class="line"><span class="number">31</span> </div><div class="line"><span class="number">32</span> <span class="comment">//日志类型字符串数组，下标为各字符串所对应的日志类型枚举值。</span></div><div class="line"><span class="number">33</span> <span class="keyword">static</span> <span class="keyword">const</span> INT8U* paLogTypeName[] = &#123;</div><div class="line"><span class="number">34</span>     <span class="string">"Norm"</span>,        <span class="string">"Frame"</span>,   <span class="string">"Pon"</span>,  <span class="string">"Ethernet"</span>,  <span class="string">"Internet"</span>,</div><div class="line"><span class="number">35</span>     <span class="string">"Multicast"</span>,   <span class="string">"Qos"</span>,     <span class="string">"Ces"</span>,  <span class="string">"Voip"</span>,      <span class="string">"Alarm"</span>,</div><div class="line"><span class="number">36</span>     <span class="string">"Performance"</span>, <span class="string">"Version"</span>, <span class="string">"Xdsl"</span>,  <span class="string">"Db"</span></div><div class="line"><span class="number">37</span> &#125;;</div><div class="line"><span class="number">38</span> <span class="keyword">static</span> <span class="keyword">const</span> INT8U  ucLogTypeNameNum = <span class="keyword">sizeof</span>(paLogTypeName) / <span class="keyword">sizeof</span>(paLogTypeName[<span class="number">0</span>]);</div><div class="line"><span class="number">39</span> </div><div class="line"><span class="number">40</span> <span class="function"><span class="keyword">static</span> VOID <span class="title">SetGlobalLogCtrl</span><span class="params">(E_LOG_TYPE eLogType, INT8U ucLogSwitch)</span></span></div><div class="line">41 &#123;</div><div class="line"><span class="number">42</span>     <span class="keyword">if</span>(LOG_ON == ucLogSwitch)</div><div class="line"><span class="number">43</span>         gOmciLogCtrl = LOG_ALL;</div><div class="line"><span class="number">44</span>     <span class="keyword">else</span></div><div class="line"><span class="number">45</span>         gOmciLogCtrl = <span class="number">0</span>;</div><div class="line"><span class="number">46</span> &#125;</div><div class="line"><span class="number">47</span> <span class="function"><span class="keyword">static</span> VOID <span class="title">SetSpecificLogCtrl</span><span class="params">(E_LOG_TYPE eLogType, INT8U ucLogSwitch)</span></span></div><div class="line">48 &#123;</div><div class="line"><span class="number">49</span>     <span class="keyword">if</span>(LOG_ON == ucLogSwitch)</div><div class="line"><span class="number">50</span>         SET_BIT(gOmciLogCtrl, eLogType);</div><div class="line"><span class="number">51</span>     <span class="keyword">else</span></div><div class="line"><span class="number">52</span>         CLR_BIT(gOmciLogCtrl, eLogType);</div><div class="line"><span class="number">53</span> &#125;</div><div class="line"><span class="number">54</span> </div><div class="line"><span class="number">55</span> <span class="function">VOID <span class="title">OmciLogCtrl</span><span class="params">(CHAR *pszLogType, INT8U ucLogSwitch)</span></span></div><div class="line">56 &#123;</div><div class="line"><span class="number">57</span>     <span class="keyword">if</span>(<span class="number">0</span> == strncasecmp(pszLogType, <span class="string">"All"</span>, LOG_TYPE_CMP_LEN))</div><div class="line"><span class="number">58</span>     &#123;</div><div class="line"><span class="number">59</span>         SetGlobalLogCtrl(LOG_ALL, ucLogSwitch);</div><div class="line"><span class="number">60</span>         <span class="keyword">return</span>;</div><div class="line"><span class="number">61</span>     &#125;</div><div class="line"><span class="number">62</span> </div><div class="line"><span class="number">63</span>     INT8U ucIdx = <span class="number">0</span>;</div><div class="line"><span class="number">64</span>     <span class="keyword">for</span>(ucIdx = <span class="number">0</span>; ucIdx &lt; ucLogTypeNameNum; ucIdx++)</div><div class="line"><span class="number">65</span>     &#123;</div><div class="line"><span class="number">66</span>         <span class="keyword">if</span>(<span class="number">0</span> == strncasecmp(pszLogType, paLogTypeName[ucIdx], LOG_TYPE_CMP_LEN))</div><div class="line"><span class="number">67</span>         &#123;</div><div class="line"><span class="number">68</span>             SetSpecificLogCtrl(ucIdx, ucLogSwitch);</div><div class="line"><span class="number">69</span>             <span class="built_in">printf</span>(<span class="string">"LogType: %s, LogSwitch: %s\n"</span>, paLogTypeName[ucIdx],</div><div class="line"><span class="number">70</span>                    (<span class="number">1</span>==ucLogSwitch)?<span class="string">"On"</span>:<span class="string">"Off"</span>);</div><div class="line"><span class="number">71</span>             <span class="keyword">return</span>;</div><div class="line"><span class="number">72</span>         &#125;</div><div class="line"><span class="number">73</span>     &#125;</div><div class="line"><span class="number">74</span> </div><div class="line"><span class="number">75</span>     OmciLogHelp();</div><div class="line"><span class="number">76</span> &#125;</div></pre></td></tr></table></figure>
<blockquote>
<p>Booch的《面向对象分析与设计》一书中，提到所有的程序设计语言大概有3个源流：结构化编程；面向对象编程；数据驱动编程.我认为数据驱动编程的本质是“参数化抽象”的思想，不同于OO的“规范化抽象”的思想</p>
<ol>
<li>数据驱动编程在网络游戏开发过程中很常用，但是少有人专门提到这个词</li>
<li>数据驱动编程有很多名字：元编程，解释器/虚拟机，LOP/微语言/DSL等。包括声明式编程、标记语言、甚至所见即所得的拖放控件，都算是数据驱动编程的一种吧</li>
<li>数据驱动编程可以帮助处理复杂性，和结构化编程、OO 均可相容。(正交的角度)</li>
<li>将变和不变的部分分离,策略和机制分离,由此联想到的还有 数据和代码的分离 微语言和解释器的分离 被生成代码和代码生成器的分离 更近一步 微内核插件式体系结构</li>
<li>元编程应该说是更加泛化的数据驱动编程 元编程不是新加入一个间接层 而是退居一步 使得当前的层变成一个间接层 元编程分为静态元编程(编译时)和动态元编程(运行时) 静态元编程本质上是一种 代码生成技术或者编译器技术 动态元编程一般通过解释器(或虚拟机) 加以实现</li>
<li>数据驱动编程当然也不应该说是”反抽象的”,但的确与”OO抽象”的思维方式是迥然不同 泾渭分明的,如TAOUP一书中所述:在Unix的模块化传统和围绕OO语言发展起来的使用模式之间,存在着紧张的对立关系”应该说数据驱动编程的思路与结构化编程和OO是正交的<br>编程和人的关系,更类似一种”跳出三界外,不在五行中”的做法.</li>
</ol>
</blockquote>
<p>​     人类心智的限制，一切的背后都有人的因素作为依据：</p>
<p>​     a 人同时关注的信息数量：7+-2 (所以要分模块)</p>
<p>​     b 人接收一组新信息的平均时间5s (所以要简单，系统总的模块数不要太多)</p>
<p>​     c 人思维的直观性(人的视觉能力和模糊思维能力)，这意味这两点：</p>
<p>​     A “直”——更善于思考自己能直接接触把玩的东西；(所以要“浅平透”、使用具象的设计，要尽量代码中只有顺直的流程)，</p>
<p>​     B “观”——更善于观图而不是推算逻辑；(所以要表驱动法，数据驱动编程，要UML，要可视化编程——当然MDA是太理想化了)</p>
<p>​     d 人不能持续集中注意力(人在一定的代码行数中产生的bug数量的比例是一定的，所以语言有具有表现力，要体现表达的经济性)</p>
<p>​     所以要机制与策略分离，要数据和代码分离(数据驱动编程)，要微语言，要DSL，要LOP……</p>
<p>​     e 人是有创造欲，有现实利益心的(只要偶可能总是不够遵从规范，或想创造规范谋利——只要成本能承受，在硬件领域就不行)</p>
<p>​     另外，开一个有意思的玩笑，Unix编程艺术艺术的英文缩写为TAOUP，我觉得可以理解为UP之TAO——向上抛出之道——将复杂的易变的逻辑作为数据或更高层代码抛给上层！</p>
<h2 id="3-2-函数指针"><a href="#3-2-函数指针" class="headerlink" title="3.2 函数指针"></a>3.2 函数指针</h2><p>​     “消息处理”一节示例中的函数指针有点插件结构的味道。可对这些插件进行方便替换，新增，删除，从而改变程序的行为。而这种改变，对事件处理函数的查找又是隔离的(隔离变化)。</p>
<p>​     函数指针非常有用，但使用时需注意其缺陷：无法检查参数(parameter)和返回值(return value)的类型。因为函数已经退化成指针，而指针不携带这些类型信息。缺少类型检查，当参数或返回值不一致时，可能会造成严重的错误。</p>
<p>​     例如，定义三个函数，分别具有两个参数：</p>
<p>​     int max(int x, int y)  {  return x&gt;y?x:y;  }</p>
<p>​     int min(int x, int y)  {  return x&lt;y?x:y;  }</p>
<p>​     int add(int x, int y)  {  return x+y;  }</p>
<p>​     而处理函数却定义为：</p>
<p>​     int process(int x, int y, int (<em>f)())  {  return (</em>f)(x, y);  }</p>
<p>​     其中，第三个参数是一个没有参数且返回int型变量的函数指针。但后面却用process(a,b,max)的方式进行调用，max带有两个参数。若编译器未检查出错误，而又不小心将return (<em>f)(x,y);写成return (</em>f)(x);，那么后果可能很严重。</p>
<p>​     因此在C语言中使用函数指针时，一定要小心”类型陷阱”。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/C技术/" rel="tag"># C技术</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/12/代码大全阅读记录/" rel="next" title="代码大全阅读记录">
                <i class="fa fa-chevron-left"></i> 代码大全阅读记录
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/19/stm32-寄存器配置/" rel="prev" title="stm32 寄存器配置">
                stm32 寄存器配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="linghaibin" />
          <p class="site-author-name" itemprop="name">linghaibin</p>
           
              <p class="site-description motion-element" itemprop="description">不要停止阅读</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/dh-linghaibin" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#编程的核心是数据结构，而不是算法"><span class="nav-number">1.</span> <span class="nav-text">编程的核心是数据结构，而不是算法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#函数表驱动法"><span class="nav-number">2.</span> <span class="nav-text">函数表驱动法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#字符统计"><span class="nav-number">2.0.1.</span> <span class="nav-text">字符统计</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-函数指针"><span class="nav-number">2.1.</span> <span class="nav-text">3.2 函数指针</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">linghaibin</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
